/**
 * 
 */
package uet.sort.algorithms;

/**
 * @author xuanh_000
 * 
 */
public class QuickSort extends Sort {
	private SequenceNumText sequenceNumText = new SequenceNumText();	//class sequence text 
	private int countSwap = 1;	//log sequence

	public QuickSort(int max) {
		super(max);
	}

	public QuickSort(long[] data) {
		super(data);
	}

	public void run(int left, int right) {
		if (left >= right)
			return;
		logSort += "\n" + sequenceNumText.getNumText(countSwap++)+"\n";	//log
		
		int k = (left + right) / 2;
		long temp = data[k];
		swap(k, right);
		int i = left, j = right - 1;
		String lineLogTemp="";
		while (i <= j) {
			lineLogTemp = stringData();	//log step level 1
			while (i <= right && data[i] <= temp)
				i++;
			while (j >= left && data[j] > temp)
				j--;
			String logSwap = "";	//log step swap
			if (i <= j) {
				logSwap = " Swap since " + data[i] + ">" + temp 
						+ ">=" + data[j];	//log
				swap(i, j);
			}
			lineLogTemp += stringData() + logSwap + "\n";	//log
			logSort += lineLogTemp;		//end log
		}
		lineLogTemp = stringData();	//log step level 1
		swap(j + 1, right);
		lineLogTemp += stringData() + " Swap "+data[right]+" and " + data[j+1];
		logSort += lineLogTemp+"\n";		//end log
		run(left, j);
		run(j + 2, right);
	}
}
